package main;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;
import java.util.ArrayList;

import scenario.*;
import unit.*;
import equipment.*;

public class UnitSelecter {
	
	public Unit goodSelection = null;
	public Unit badSelection = null;
	
	public Label goodStats = new Label("");
	public Label badStats = new Label("");
	
	public Label goodEquipment = new Label("");
	public Label badEquipment = new Label("");
	
	public static int id = 0;
	
	public UI myUI;
	
	public UnitSelecter(UI inputUI) {
		if (inputUI.game.armies[0] != null) {
			inputUI.beginGame();
			return;
		}
		myUI = inputUI;

		Label goodHeader = new Label(Unit.tabbedHeader());
		goodHeader.setName("header");
		goodHeader.setBounds(25, 180, 150,20);
		Label badHeader = new Label(Unit.tabbedHeader());
		badHeader.setName("header");
		badHeader.setBounds(myUI.dim.width/2+25, 180, 150,20);
		
		goodStats.setBounds(25,200, 150,20);
		goodStats.setName("stats");
		badStats.setBounds(myUI.dim.width/2+25, 200, 150,20);
		badStats.setName("stats");
		
		goodEquipment.setBounds(25,230,myUI.dim.width/2-50,20);
		goodEquipment.setName("stats");
		badEquipment.setBounds(myUI.dim.width/2+25,230,myUI.dim.width/2-50,20);
		badEquipment.setName("stats");
		
		final ArrayList<Unit> goodUnits = new ArrayList<Unit>();
		final ArrayList<Unit> badUnits = new ArrayList<Unit>();
		
		final Choice goodChoice = new Choice();
		goodChoice.setName("goodChoice");
		goodChoice.setBounds(25, 50, 100, 50);
		final Choice badChoice = new Choice();
		badChoice.setName("badChoice");
		badChoice.setBounds(myUI.dim.width/2+25, 50, 100, 50);
		
		// Good units
		final Class[] goodArray = {HighElf.class, WarriorMinasTirith.class};
		// Bad units
		final Class[] badArray = {Goblin.class, Orc.class};
		
		// Populate choices
		for (Class c : goodArray){
			goodChoice.add(c.getSimpleName());
		}
		for (Class c : badArray){
			badChoice.add(c.getSimpleName());
		}
		
		Button inspectGoodUnit = new Button("Inspect Good Unit");
		inspectGoodUnit.setName("inspect");
		inspectGoodUnit.setBounds(25, 100, 100, 50);
		inspectGoodUnit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean up previous equipment stuff
				myUI.removeByName("equipGood");
				
				// Get a new unit for the selected Unit
				Object[] args = {new Integer(0), new Integer(0), new Integer(0), new Integer(0)};
				try {goodSelection = (Unit)goodArray[goodChoice.getSelectedIndex()].getConstructors()[0].newInstance(args);
				} catch (Exception exc) {
					exc.printStackTrace();}
				updateLabels();
				
				// Add new equip stuff
				Choice equipGood = new Choice();
				equipGood.setLocation(25,250);
				equipGood.setName("equipGood");
				System.out.println(goodSelection.getClass());
				System.out.println(goodSelection.equipChoices.length);
				
				for (Equipment equip : goodSelection.equipChoices) {
					equipGood.add(equip.getClass().getSimpleName());
				}
				System.out.println(equipGood.countItems());
				myUI.add(equipGood);
			}
		});
		
		Button inspectBadUnit = new Button("Inspect Bad Unit");
		inspectBadUnit.setName("inspect");
		inspectBadUnit.setBounds(myUI.dim.width/2+25, 100, 100, 50);
		inspectBadUnit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean up previous equipment stuff
				myUI.removeByName("equipGood");
				
				// Get a new unit for the selected Unit
				Object[] args = {new Integer(0), new Integer(1), new Integer(0), new Integer(0)};
				try {badSelection = (Unit)badArray[badChoice.getSelectedIndex()].getConstructors()[0].newInstance(args);
				} catch (Exception exc) {
					exc.printStackTrace();}
				updateLabels();
			}
		});
		
		
		
		final Button done = new Button("Ready to Fight!");
		done.setName("done");
		done.setBounds(myUI.dim.width-100, 0, 100, 50);
		done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean everything up
				myUI.removeByName("done");
				
				// Start the game
				myUI.beginGame();
			}
		});
		
		Button place = new Button("Place Units");
		place.setName("place");
		place.setBounds(myUI.dim.width-100, 0, 100, 50);
		place.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean myself and selection up
				myUI.removeByName("place");
				myUI.removeByName("goodChoice");
				myUI.removeByName("badChoice");
				myUI.removeByName("inspect");
				myUI.removeByName("header");
				myUI.removeByName("stats");
				
				// Add done button and PlaceListener
				myUI.add(done);
			}
		});
		
		
		myUI.add(goodChoice);
		myUI.add(badChoice);
		myUI.add(inspectGoodUnit);
		myUI.add(inspectBadUnit);
		
		myUI.add(goodHeader);
		myUI.add(badHeader);
		myUI.add(goodStats);
		myUI.add(badStats);
		myUI.add(goodEquipment);
		myUI.add(badEquipment);
		
		myUI.add(place);
		
		myUI.revalidate();
		myUI.repaint();
	}
	
	public void updateLabels(){
		if (goodSelection != null) {
			goodStats.setText(goodSelection.tabbedStats());
			goodEquipment.setText("Equipment: " + goodSelection.equipmentString());
		}
		else {
			goodStats.setText("");
			goodEquipment.setText("");
		}
		if (badSelection != null) {
			badStats.setText(badSelection.tabbedStats());
			badEquipment.setText("Equipment: " + badSelection.equipmentString());
		}
		else {
			badStats.setText("");
			badEquipment.setText("");
		}
		myUI.repaint();
	}
	
}